<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>列表过滤</title>
    <script src="../../js/vue.js"></script>
    <style>
        *{
            font-size: 18px;
            font-weight: 700;
        }
    </style>
</head> 
<body>
    <div id="root">
        <h4>人员列表</h4>   
        搜索框：<input type="text" v-model='inputKey' placeholder="请输入搜索信息">
        <ul>
            <li v-for='person in newPersonList' :key="person.id" >
                {{person.name}}-{{person.age}}--{{person.sex}}
            </li>
        </ul>

        
    </div>
    <script type="text/javascript">
        Vue.config.productionTip = false;
        const vm = new Vue({
            el: '#root',
            data: {
                personList: [
                    {id: 001, name: '周冬雨',age:22, sex: '女'},
                    {id: 002, name: '马冬梅',age:29, sex: '女'},
                    {id: 003, name: '周杰伦',age:31, sex: '男'},
                    {id: 004, name: '温兆伦',age:46, sex: '男'},
                    {id: 005, name: '陈道明',age:51, sex: '男'},
                    {id: 006, name: '姜文',age:46, sex: '男'},
                    {id: 007, name: '姜武',age:44, sex: '男'},
                    {id: '008', name: '梅超风',age:66, sex: '女'}
                ],
                inputKey: ''
            },


            // 计算属性实现人员搜索

            computed: {
                newPersonList(){
                    return this.personList.filter((p)=>{
                        return p.name.indexOf(this.inputKey) !== -1
                    })
                }
            },

            // 数据监视实现  人员搜索
            // watch: {
            //     inputKey: {
            //         immediate:true,  // 页面初始化时就调用一次   name.indexOf('')  所有字符串中都包含''字符串  所以会过滤出所有的信息
            //         handler(val){
            //             this.newPersonList = this.personList.filter((p)=>{
            //                 return p.name.indexOf(val) !== -1
            //             })
            //         }
            //     }
            // },
            
        })
    </script>
</body>
</html>